package com.example.park2212.news

import androidx.lifecycle.MutableLiveData
import com.example.lib_base.entity.NewsBean
import com.example.lib_base.entity.NewsBeanItem
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(val apiservice: ApiService) : BaseViewModel() {

    val getNewsSuccess = MutableLiveData<NewsBean>()
    fun getNews(){
        httpRequest({
            apiservice.getNews()
        },getNewsSuccess::postValue)
    }

    val addNewsSuccess = MutableLiveData<NewsBeanItem>()
    fun addNews(item: NewsBeanItem){
        httpRequest({
            apiservice.addNews(mapToBody(item))
        },addNewsSuccess::postValue)
    }

    val newsSearchSuccess = MutableLiveData<NewsBean>()
    fun newsSearch(keyword:String){
        httpRequest({
            apiservice.newsSearch(keyword)
        },newsSearchSuccess::postValue)
    }

    val newsDeleteSuccess = MutableLiveData<Boolean>()
    fun newsDelete(id:Int){
        httpRequest({
            apiservice.newsDelete(id)
        },newsDeleteSuccess::postValue)
    }
}